package com.leen.framework.controller;

import com.alibaba.fastjson.JSONObject;

import com.leen.common.vo.ResultVO;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 生成验证图形验证码的控制器
 *
 * @author admin
 * @date 2021-05-07
 */
@RestController
@RequestMapping(value = "/common")
public class CaptchaController {

    @Autowired
    private StringRedisTemplate  stringRedisTemplate;

    /**
     * 生成gif验证码
     * @param width
     * @param height
     * @param len
     * @return
     */
    @GetMapping(value = "/captcha/gif")
    public ResultVO<JSONObject> createGif(@RequestParam(name = "width",required = false,defaultValue = "120") int width,
                                          @RequestParam(name = "height",required = false,defaultValue = "38") int height,
                                          @RequestParam(name = "len",required = false,defaultValue = "4") int len){
        GifCaptcha captcha = new GifCaptcha(width, height);
        // 几位数运算，默认是两位
        captcha.setLen(len);
        //得到结果 (存到redis中的 value)
        String code= captcha.text();
        //验证码设定一下过期时间 redis  3098020f-96fd-474b-a64e-6d8e36eef929
        String key=  UUID.randomUUID().toString().replace("-","");
        //存放到redis中，10分钟过期
        this.stringRedisTemplate.opsForValue().set("captcha:"+ key,code,10L, TimeUnit.MINUTES);

        //创建一个对象
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("key", key);
        //得到图片 （Base64位字符串）
        jsonObject.put("image",captcha.toBase64());

        return ResultVO.success(jsonObject);

    }

    /**
     * 生成png类型验证码
     * @param width
     * @param height
     * @param len
     * @return
     */
    @GetMapping(value = "/captcha/png")
    public ResultVO<JSONObject> createPng(@RequestParam(name = "width",required = false,defaultValue = "120") int width,
                                          @RequestParam(name = "height",required = false,defaultValue = "38") int height,
                                          @RequestParam(name = "len",required = false,defaultValue = "4") int len){
        SpecCaptcha captcha = new SpecCaptcha(width, height);
        // 几位数运算，默认是两位
        captcha.setLen(len);
        //得到结果 (存到redis中的 value)
        String code= captcha.text();
        //验证码设定一下过期时间 redis  3098020f-96fd-474b-a64e-6d8e36eef929
        String key=  UUID.randomUUID().toString().replace("-","");
        //存放到redis中，10分钟过期
        this.stringRedisTemplate.opsForValue().set("captcha:"+ key,code,10L, TimeUnit.MINUTES);

        //创建一个对象
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("key", key);
        //得到图片 （Base64位字符串）
        jsonObject.put("image",captcha.toBase64());

        return ResultVO.success(jsonObject);

    }

    /**
     * 生成算术类型验证码
     * @param width
     * @param height
     * @return
     */
    @GetMapping(value = "/captcha/arit")
    public ResultVO<JSONObject> createArit(@RequestParam(name = "width",required = false,defaultValue = "120") int width,
                                          @RequestParam(name = "height",required = false,defaultValue = "38") int height){
        ArithmeticCaptcha captcha =  new ArithmeticCaptcha(width, height);

        //得到结果 (存到redis中的 value)
        String code= captcha.text();
        //验证码设定一下过期时间 redis  3098020f-96fd-474b-a64e-6d8e36eef929
        String key=  UUID.randomUUID().toString().replace("-","");
        //存放到redis中，10分钟过期
        this.stringRedisTemplate.opsForValue().set("captcha:"+ key,code,10L, TimeUnit.MINUTES);

        //创建一个对象
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("key", key);
        //得到图片 （Base64位字符串）
        jsonObject.put("image",captcha.toBase64());

        return ResultVO.success(jsonObject);

    }
}
